package com.ming.controller;

import com.ming.VO.ResultVO;
import com.ming.converter.OrderForm2OrderDTOConverter;
import com.ming.dto.OrderDTO;
import com.ming.enums.ResultEnum;
import com.ming.exception.SellException;
import com.ming.form.OrderForm;
import com.ming.service.BuyerService;
import com.ming.service.OrderService;
import com.ming.utils.ResultVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LIANG on 2018/3/19.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;
    //创建订单ddd
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确，orderForm={}",orderForm);
            throw new SellException(ResultEnum.ORDER_STATUS_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("【创建订单，】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO createResult = orderService.create(orderDTO);
        Map<String,String> map = new HashMap<>();
        map.put("orderId",createResult.getOrderId());
        return ResultVoUtil.success(map);
    }
    //查询订单列表
    @RequestMapping("/list")
    public ResultVO<Page<OrderDTO>> list(@RequestParam("openid")String openid,
                                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10")Integer size){
            if (StringUtils.isEmpty(openid)){
                log.error("【查询订单列表】，openid为空");
                throw  new SellException(ResultEnum.PARAM_ERROR);
            }
            PageRequest pageRequest = new PageRequest(page,size);
            Page<OrderDTO> orderDTOList = orderService.findList(openid,pageRequest);
            return  ResultVoUtil.success(orderDTOList.getContent());
        }
    //查看订单详情
    @RequestMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid")String openid,
                                     @RequestParam("orderid")String orderid){
        OrderDTO  orderDTO =buyerService.findOrderOne(openid,orderid);
        return ResultVoUtil.success(orderDTO);
    }
    //取消订单
    @RequestMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid")String openid,
                           @RequestParam("orderid")String orderid){
        buyerService.cancelOrder(openid,orderid);
        return ResultVoUtil.success();
    }

}
